﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using SQLiteClient;

namespace Vanglen.JBJLQ
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            Common.CopyContentToIsolatedStorage("db/dbrecords.db");
            Common.CopyContentToIsolatedStorage("db/dbrecords.db-journal");

            //SQLiteClient.SQLiteConnection db = new SQLiteConnection("dbrecords.db");
            //db.Open();

            //SQLiteCommand cmd = db.CreateCommand(" CREATE TABLE records (id INTEGER PRIMARY KEY, name TEXT, date TEXT, timebegin TEXT, timeend TEXT, reason TEXT, note TEXT); ");
            //cmd.ExecuteNonQuery();

            //db.Dispose();
            //db = null;
        }

        private void ApplicationBarMenuItem_Click(object sender, EventArgs e)
        {
            CustomMessageBox messageBox = new CustomMessageBox()
            {
                Caption = "关于我们",
                Message = "如果您对本产品有任何的意见或建议，欢迎联系我：Email:omicle@sina.com",
                LeftButtonContent = "OK",
                IsRightButtonEnabled = false,
                IsFullScreen = true
            };
            messageBox.Show();
        }

        private void ApplicationBarIconButtonSave_Click(object sender, EventArgs e)
        {
            try
            {
                this.Save();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void ApplicationBarIconButtonClear_Click(object sender, EventArgs e)
        {
            try
            {
                this.ClearData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void ApplicationBarIconButtonDetial_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Detail.xaml", UriKind.RelativeOrAbsolute));
        }

        private void Save()
        {
            Records r = new Records();
            r.date = tbDate.Value.Value.ToString("yyyy-MM-dd");
            r.timebegin = tbTimeBegin.Value.Value.ToString("HH:mm:ss");
            r.timeend = tbTimeEnd.Value.Value.ToString("HH:mm:ss");
            r.reason = tbReason.Text;
            r.note = tbNote.Text;

            using (SQLiteHelper helper = new SQLiteHelper())
            {
                string sql = "insert into records (date, timebegin, timeend, reason, note) values(@date,@timebegin,@timeend,@reason,@note) ";
                int result = helper.ExecuteNonQuery<Records>(sql, r);
                if (result > 0)
                    MessageBox.Show("保存成功！");
                ClearData();
            }
        }

        private void ClearData()
        {
            this.tbDate.Value = DateTime.Now;
            this.tbTimeBegin.Value = DateTime.Now;
            this.tbTimeEnd.Value = DateTime.Now;
            this.tbReason.Text = "";
            this.tbNote.Text = "";
            this.tbDate.Focus();
        }
    }
}